# -*- coding: utf-8 -*-
"""
使用思维导图/树形图展示红楼梦人物关系
"""

from pyecharts import options as opts
from pyecharts.charts import Tree

# 红楼梦人物关系树状数据结构
# 以贾府为中心的家族关系树
data = [
    {
        "name": "贾府",
        "children": [
            {
                "name": "宁国府",
                "children": [
                    {
                        "name": "贾珍",
                        "children": [
                            {"name": "贾蓉"},
                            {"name": "秦可卿"}
                        ]
                    }
                ]
            },
            {
                "name": "荣国府",
                "children": [
                    {
                        "name": "贾母",
                        "children": [
                            {
                                "name": "贾政",
                                "children": [
                                    {
                                        "name": "贾宝玉",
                                        "children": [
                                            {"name": "袭人"},
                                            {"name": "晴雯"},
                                            {"name": "麝月"}
                                        ]
                                    },
                                    {
                                        "name": "贾元春"
                                    },
                                    {
                                        "name": "贾探春"
                                    },
                                    {
                                        "name": "贾环"
                                    }
                                ]
                            },
                            {
                                "name": "贾赦",
                                "children": [
                                    {
                                        "name": "贾琏",
                                        "children": [
                                            {"name": "王熙凤"},
                                            {"name": "巧姐"}
                                        ]
                                    },
                                    {"name": "贾迎春"}
                                ]
                            },
                            {
                                "name": "贾敏",
                                "children": [
                                    {
                                        "name": "林黛玉",
                                        "children": [
                                            {"name": "紫鹃"},
                                            {"name": "雪雁"}
                                        ]
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "name": "王夫人",
                        "children": [
                            {
                                "name": "薛姨妈",
                                "children": [
                                    {
                                        "name": "薛宝钗",
                                        "children": [
                                            {"name": "莺儿"}
                                        ]
                                    },
                                    {"name": "薛蟠"}
                                ]
                            }
                        ]
                    },
                    {
                        "name": "史太君",
                        "children": [
                            {
                                "name": "史湘云"
                            }
                        ]
                    },
                    {
                        "name": "王熙凤",
                        "children": [
                            {"name": "平儿"}
                        ]
                    },
                    {
                        "name": "李纨",
                        "children": [
                            {
                                "name": "贾兰"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

def create_character_mindmap():
    """创建红楼梦人物关系思维导图"""
    tree = (
        Tree(init_opts=opts.InitOpts(width="1400px", height="900px"))
        .add(
            "",
            data,
            collapse_interval=2,
            orient="LR",  # 从左到右布局
            initial_tree_depth=3  # 初始展开3层
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(
                title="《红楼梦》人物关系思维导图",
                subtitle="以贾府为中心的家族关系树",
                pos_left="center"
            ),
            tooltip_opts=opts.TooltipOpts(trigger="item", trigger_on="mousemove")
        )
    )
    # 设置全局提示框
    tree.set_global_opts(
        tooltip_opts=opts.TooltipOpts(trigger="item", trigger_on="mousemove")
    )
    return tree

def create_radial_mindmap():
    """创建辐射状红楼梦人物关系思维导图"""
    tree = (
        Tree(init_opts=opts.InitOpts(width="1000px", height="1000px"))
        .add(
            "",
            data,
            collapse_interval=2,
            layout="radial",  # 辐射状布局
            initial_tree_depth=3
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(
                title="《红楼梦》人物关系辐射图",
                subtitle="以贾府为中心的家族关系辐射图",
                pos_left="center"
            ),
            tooltip_opts=opts.TooltipOpts(trigger="item", trigger_on="mousemove")
        )
    )
    return tree

def main():
    print("《红楼梦》人物关系思维导图生成中...")
    
    # 创建从左到右的树状图
    tree_chart = create_character_mindmap()
    tree_chart.render("红楼梦人物关系思维导图.html")
    print("思维导图已保存为 红楼梦人物关系思维导图.html")
    
    # 创建辐射状图
    radial_chart = create_radial_mindmap()
    radial_chart.render("红楼梦人物关系辐射图.html")
    print("辐射图已保存为 红楼梦人物关系辐射图.html")
    
    # 生成说明文档
    with open("红楼梦思维导图说明.txt", "w", encoding="utf-8") as f:
        f.write("《红楼梦》人物关系思维导图说明\n")
        f.write("=" * 35 + "\n\n")
        
        f.write("一、文件说明\n")
        f.write("1. 红楼梦人物关系思维导图.html - 从左到右的树状关系图\n")
        f.write("2. 红楼梦人物关系辐射图.html - 以中心辐射状展示的关系图\n\n")
        
        f.write("二、图示说明\n")
        f.write("1. 矩形框代表人物或家族\n")
        f.write("2. 连线表示关系，父子关系表示上下级或血缘关系\n")
        f.write("3. 节点可点击展开或收起子节点\n\n")
        
        f.write("三、交互操作\n")
        f.write("1. 鼠标悬停：显示节点信息\n")
        f.write("2. 点击节点：展开/收起子节点\n")
        f.write("3. 滚轮缩放：放大缩小视图\n")
        f.write("4. 拖拽空白处：移动整个图形\n\n")
        
        f.write("四、主要家族关系\n")
        f.write("1. 贾府分为宁国府和荣国府\n")
        f.write("2. 贾母是荣国府的核心长辈\n")
        f.write("3. 贾宝玉是贾政之子，贾母最疼爱的孙子\n")
        f.write("4. 林黛玉是贾敏之女，贾母的外孙女\n")
        f.write("5. 薛宝钗是薛姨妈之女，王夫人的外甥女\n")
        f.write("6. 王熙凤是贾琏之妻，王夫人的侄女\n")
    
    print("说明文档已保存为 红楼梦思维导图说明.txt")

if __name__ == "__main__":
    main()